package com.derekandbritt.booST.data;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Challenge implements Comparable<Challenge> {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	protected Long id;
	
	protected String sender;
	protected String receiver;
	protected Date timeIssued;
	protected Date lastUpdated;
	protected Status status;
	protected Type type;
	protected Date expiration;
	protected double distance;
	protected int timeLimit;
	protected int senderTime;
	protected int receiverTime;
	
	public long getId() {
		return (id != null) ? id : 0;
	}
	public void setId(long id) {
		this.id = id;
	}
	
	public String getSender() {
		return sender;
	}
	public void setSender(String sender) {
		this.sender = sender;
	}
	
	public String getReceiver() {
		return receiver;
	}
	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}
	
	public Date getTimeIssued() {
		return timeIssued;
	}
	public void setTimeIssued(Date timeIssued) {
		this.timeIssued = timeIssued;
	}
	
	public Date getLastUpdated() {
		return lastUpdated;
	}
	public void setLastUpdated(Date lastUpdated) {
		this.lastUpdated = lastUpdated;
	}
	
	public Status getStatus() {
		return status;
	}
	public void setStatus(Status status) {
		this.status = status;
	}
	
	public Type getType() {
		return type;
	}
	public void setType(Type type) {
		this.type = type;
	}
	
	public Date getExpiration() {
		return expiration;
	}
	public void setExpiration(Date expiration) {
		this.expiration = expiration;
	}
	
	public double getDistance() {
		return distance;
	}
	public void setDistance(double distance) {
		this.distance = distance;
	}

	public int getTimeLimit() {
		return timeLimit;
	}
	public void setTimeLimit(int timeLimit) {
		this.timeLimit = timeLimit;
	}
	
	public int getSenderTime() {
		return senderTime;
	}
	public void setSenderTime(int senderTime) {
		this.senderTime = senderTime;
	}
	
	public int getReceiverTime() {
		return receiverTime;
	}
	public void setReceiverTime(int receiverTime) {
		this.receiverTime = receiverTime;
	}

    public int compareTo(Challenge c) {
        return (int)(c.id - this.id);
    }
	
    public boolean isWinner(String userId) {
        if(userId.equalsIgnoreCase(sender) && senderTime > 0)
            return senderTime > receiverTime;
        else if(userId.equalsIgnoreCase(receiver) && receiverTime > 0)
            return receiverTime > senderTime;
        return false;
    }
    
	public enum Type {
	
		COMPETITIVE(0, "Competitive"),
		COOPERATIVE(1, "Cooperative");
		
		private final int id;
		private final String name;
		
		Type(int id, String name) {
			this.id = id;
			this.name = name;
		}
		
		public int getId() {
			return id;
		}
		
		public String getName() {
			return name;
		}
		
		public String toString() {
			return name;
		}
	}
	
	public enum Status {
		PENDING,
		ACCEPTED,
		REJECTED,
		EXPIRED,
		COMPLETED;
	}
}
